<?php
/**
 * GioCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@ninhgio.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2010-2011 GioCMS (http://cms.ninhgio.com)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @author		NinhGio - ninhgio@gmail.com
 * @since		1.0
 */

class Modules_Comment_Controllers_Comment extends Gio_Core_Controller 
{
	/* ========== Frontend actions ========================================== */
	
	public function sendAction()
	{
		$this->disableLayout();
		$this->setNoRender();
		
		$request = $this->getRequest();
		if ($request->isPost()) {
			$commentData = $request->getPost('comment');
			
			$paramString = $commentData['params'];
			$json = new Services_JSON();
			$params = base64_decode($paramString);
			
			$comment = array(
				'title' 	   => $this->view->STRING->escape($commentData['title']),
				'content' 	   => $this->view->STRING->escape($commentData['content']),
				'full_name'    => $this->view->STRING->escape($commentData['full_name']),
				'email' 	   => $this->view->STRING->escape($commentData['email']),
				'phone' 	   => $this->view->STRING->escape($commentData['phone']),
				'web_site'	   => '',
				'ip' 		   => $request->getRemoteIp(),
				'created_date' => date('Y-m-d H:i:s'),
				'status'       => 'inactive',
				//'reply_to' 	   => (int)$commentData['reply_to'],
				'reply_to' 	   => 0,
				'route' 	   => $commentData['route'],
				'params'	   => $paramString,
			);
			
			$id = Modules_Comment_Services_Comment::add($comment);
			$comment['comment_id'] = $id;
			Modules_Comment_Services_Comment::reupdateOrderInThread($comment);
			
			Gio_Core_Messenger::getInstance()
								->addMessage($this->view->TRANSLATOR->translator('comment_actions_send_success'));
			$this->redirect($this->view->url($comment['route'], (array)$json->decode($params)));			
		}
	}
	
	/* ========== Backend actions =========================================== */
	
	public function activateAction()
	{
		$this->setNoRender();
		$this->disableLayout();
		
		$request = $this->getRequest();
		if (!$request->isPost()) {
			return;
		}
		$response = 'RESULT_NOT_OK';
		$commentId = $request->getPost('comment_id');
		$comment = Modules_Comment_Services_Comment::getById($commentId);
		
		if (null == $comment){
			$this->getResponse()->setBody($response);
			return;	
		}
		
		$act = $request->getPost('act');
		switch ($act) {
			case 'activate':
				$status = 'active';
				$comment['activate_date'] = date('Y-m-d H:i:s');
				break;
			case 'deactivate':
				$status = 'inactive';
				break;
		}
		$comment['status'] = $status;
		$response = 'RESULT_OK';
		Modules_Comment_Services_Comment::activate($comment);
		$this->getResponse()->setBody($response);
	}
	
	public function addAction()
	{
		
	}
	
	public function deleteAction()
	{
		
	}
	
	public function editAction()
	{
		
	}
	
	public function listAction()
	{
		$request = $this->getRequest();
		
		$perPage = 20;
		$pageIndex = (int)$request->getParam('page_index');
		if (null == $pageIndex || '' == $pageIndex || $pageIndex < 0) {
			$pageIndex = 1;
		}
		$start = ($pageIndex - 1) * $perPage;
		$this->view->pageIndex = $pageIndex;
		
		$condition = array('status' => null);
		$json = new Services_JSON();
		if ($request->isPost()) {
			$condition = $request->getPost('condition');
			$params = rawurlencode(base64_encode($json->encodeUnsafe($condition)));
		} else {
			$params = $request->getParam('q');
			($params != null)
				? ($condition = (array)$json->decode(rawurldecode(base64_decode($params))))
				: ($params = rawurlencode(base64_encode($json->encodeUnsafe($condition))));
		}
		$params = (empty($condition)) ? null : $params;
		$this->view->condition = $condition;
		
		$comments = Modules_Comment_Services_Comment::find($start, $perPage, $condition);
		$numComments = Modules_Comment_Services_Comment::count($condition);
		
		$this->view->comments = $comments; 
		
		// Pager
		require_once LIB_DIR . DS . 'PEAR' . DS . 'Pager' . DS . 'Sliding.php';
		$pagerPath = $this->view->url('comment_comment_list');
		$pagerOptions = array(
			'mode'       	=> 'Sliding',
			'append'   		=> false,
    		'perPage'    	=> $perPage,
    		'delta'      	=> 3,
			'urlVar'		=> 'page',
			'path'			=> $pagerPath,
			'fileName'		=> (null == $params) ? 'page-%d' : 'page-%d' . '/?q=' . $params,
			'separator'		=> '',
			'nextImg' 		=> '<small class="icon arrow_right"></small>',
			'prevImg'		=> '<small class="icon arrow_left"></small>',
			'altNext'		=> '',
			'altPrev'		=> '',
			'altPage'		=> '',
    		'totalItems' 	=> $numComments,
			'currentPage'	=> $pageIndex,
			'urlSeparator'	=> '/',
			'spacesBeforeSeparator' => 0,
			'spacesAfterSeparator' => 0,
			'curPageSpanPre' => '<a href="javascript: void();" class="current">',
			'curPageSpanPost' => '</a>'
		
		);
		$pager = new Pager_Sliding($pagerOptions);
		$this->view->pager = $pager;
	}
	
	public function threadAction()
	{
		$request = $this->getRequest();
		
		$perPage = 20;
		$pageIndex = (int)$request->getParam('page_index');
		if (null == $pageIndex || '' == $pageIndex || $pageIndex < 0) {
			$pageIndex = 1;
		}
		$start = ($pageIndex - 1) * $perPage;
		$this->view->pageIndex = $pageIndex;
		
		$paramsString = $request->getParam('params');
		
		$comments  = Modules_Comment_Services_Comment::getThreadComments($start, $perPage, $paramsString);
		$total 	   = Modules_Comment_Services_Comment::countThreadComments($paramsString);
		print_r($comments); die();
		$this->view->comments = $comments; 
		
		// Pager
		require_once LIB_DIR . DS . 'PEAR' . DS . 'Pager' . DS . 'Sliding.php';
		$pagerPath = $this->view->url('comment_comment_thread', array('params' => $paramsString));
		$pagerOptions = array(
			'mode'       	=> 'Sliding',
			'append'   		=> false,
    		'perPage'    	=> $perPage,
    		'delta'      	=> 3,
			'urlVar'		=> 'page',
			'path'			=> $pagerPath,
			'fileName'		=> 'page-%d',
			'separator'		=> '',
			'nextImg' 		=> '<small class="icon arrow_right"></small>',
			'prevImg'		=> '<small class="icon arrow_left"></small>',
			'altNext'		=> '',
			'altPrev'		=> '',
			'altPage'		=> '',
    		'totalItems' 	=> $total,
			'currentPage'	=> $pageIndex,
			'urlSeparator'	=> '/',
			'spacesBeforeSeparator' => 0,
			'spacesAfterSeparator' => 0,
			'curPageSpanPre' => '<a href="javascript: void();" class="current">',
			'curPageSpanPost' => '</a>'
		
		);
		$pager = new Pager_Sliding($pagerOptions);
		$this->view->pager = $pager;
	}
}